package controller;

import bean.Users;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CaCheService;
import service.LoginRegistrationService;
import util.ParameterUtils;
import util.ResponseBean;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;


@Controller
@Validated
public class LoginRegistration {

    private static final Logger logger = LoggerFactory.getLogger(LoginRegistration.class);

    @Resource
    private LoginRegistrationService loginRegistrationService;

    @Resource
    private CaCheService caCheService;

    @Resource
    private ResponseBean responseBean;


    /**
     * 登录
     * @param phone 手机号码
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean login(@NotBlank @Length(min = 11, max = 11) String phone, @NotBlank @Length(min = 6) String password) {

        return loginRegistrationService.login(phone, password);

    }

    /**
     * 注册
     * @param users         用户对象
     * @param bindingResult 校验异常
     * @param code          验证码
     * @return
     */
    @RequestMapping(value = "/redis", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean redis(@Validated Users users, BindingResult bindingResult, @NotBlank @Length(min = 6, max = 6) String code) throws Exception{
        //用户名 密码 电话号码 性别

        //Bean参数有误
        if (bindingResult.hasErrors()) {
            responseBean = new ResponseBean(ResponseBean.HTTP_CODE_BAD_REQUEST, ResponseBean.MSG_PARAMETER_ERROR_MESSAGE, null);

        } else {
//            读取缓存中验证码
            String codeCache = caCheService.putPhoneCode(users.getPhone());

            try {
                //校验数据
                responseBean = loginRegistrationService.redis(users, code, codeCache);
            } catch (DuplicateKeyException e) {
                //插入数据库 唯一性字段出现重复
                //因为排除user_id和Identification 都使用了UUID 出现重复几率极低 不作处理
                //仅处理手机号码出现重复,一般情况很少人会用相同手机号码频繁注册一个账号
                //重复唯一性异常
                //返回数据给前台：手机号码已重复
                //先插入后捕捉的方式在一定程度上效率得到得提升(该异常在非恶意情况下) 可以缩短将近百分之十的时间
                e.printStackTrace();
                responseBean.setAll(ResponseBean.HTTP_CODE_BAD_REQUEST, "Mobile phone number has been used", null);

                return responseBean;
            }
            //删除验证码缓存
            if (responseBean.getCode() == ResponseBean.HTTP_CODE_OK) {
                caCheService.dletePhoneCode(users.getPhone());
            }


        }

        return responseBean;

    }

//    //登录注册出现空指针异常
//    @ExceptionHandler(NullPointerException.class)
//    public ResponseBean LoginRegistrationNullPointerException(Exception e) {
//        ResponseBean responseBean = new ResponseBean();
//
//
//        return responseBean;
//    }


}
